package me.leiusual.sharing.ui.shop.adapter

import android.view.View
import androidx.core.view.isVisible
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.extensions.LayoutContainer
import kotlinx.android.synthetic.main.view_holder_shop_withdraw.*
import me.leiusual.sharing.R
import me.leiusual.sharing.api.model.Withdraw
import me.leiusual.sharing.util.extension.toSpanned

/**
 * author: leiusual
 * create on: 2019-08-01
 * description:
 */
class ShopWithdrawViewHolder(
    override val containerView: View,
    private val applyListener: (Withdraw) -> Unit,
    private val cancelListener: (Withdraw) -> Unit
) : RecyclerView.ViewHolder(containerView), LayoutContainer, View.OnClickListener {

  lateinit var withdraw: Withdraw

  init {
    button_cancel.setOnClickListener(this)
  }

  fun bind(withdraw: Withdraw) {
    this.withdraw = withdraw

    // 账户类型：红包账户\n提现金额：￥100\n服务费用：￥5\n审核状态：审核成功\n入账账号：124135123535234235 / 张三\n提现状态：转款中
    val builder = StringBuilder()

    builder.append("""账户类型：""")
    builder.append(withdraw.accountTypeName)

    builder.append("""<br/>提现金额：""")
    builder.append("""<font color="black">￥${withdraw.amount}</font>""")

    builder.append("""<br/>服务费用：""")
    builder.append("""￥${withdraw.serviceFee}""")

    builder.append("""<br/>审核状态：""")
    builder.append(withdraw.auditStatusName)

    if (withdraw.auditStatus == Withdraw.AuditStatus.AUDIT_FAIL) {
      builder.append("""<br/>审核备注：""")
      builder.append(withdraw.auditNote)
    }

    builder.append("""<br/>入账账号：""")
    builder.append(withdraw.bankCardId + """ / """ + withdraw.cardOwnerName)

    builder.append("""<br/>提现状态：""")
    builder.append("""<font color="red">${withdraw.statusText}</font>""")

    if (withdraw.withdrawTime != null) {
      builder.append("""<br/>转款时间：""")
      builder.append(withdraw.withdrawTime)
    }

    builder.append("""<br/>申请时间：""")
    builder.append(withdraw.createTime)

    text_view.text = builder.toString().toSpanned()

    // 申请审核
    button_apply.isVisible = withdraw.auditStatus == Withdraw.AuditStatus.NOT_APPLY

    // 撤回
    button_cancel.isVisible = withdraw.auditStatus in arrayOf(Withdraw.AuditStatus.AUDITING, Withdraw.AuditStatus.AUDIT_FAIL)
  }

  override fun onClick(v: View?) {
    when (v?.id) {
      R.id.button_apply -> {
        applyListener(withdraw)
      }
      R.id.button_cancel -> {
        cancelListener(withdraw)
      }
    }
  }
}